/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function (p, q) {
  let result = true;

  var diffTree = function (p, q) {
    if (p == null && q == null) {
      return;
    }

    if (p == null || q == null) {
      result = false;
      return;
    }

    if (!result || p.val !== q.val) {
      result = false;
      return;
    }

    diffTree(p.left, q.left);
    diffTree(p.right, q.right);
  };

  diffTree(p, q);

  return result;
};
